<?php

namespace App\Http\Controllers\Admin\ThreeCheck;

use App\Enums\RewardEnum;
use App\Enums\TaskEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;

class ThreeCheckController extends BaseController
{
    /**
     * @api {get} /three/check/stat 三方服务统计
     * @apiVersion 0.1.0
     * @apiName 三方服务统计
     * @apiGroup 三方服务验证
     *
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": [
    {
    "server_name 服务名称": "融云",
    "server_type 服务类型": 2,
    "is_error 是否可用": 0,
    "right_percent 可用率": 0,
    "warning_percent 警告率": 0,
    "error_percent 错误率": 0,
    "all_num 所有统计数": 0,
    "error_num 错误数": 0,
    "warning_num 警告数": 0,
    "last_error 上次警告信息": {
    "id": 1,
    "is_out_time": 2,
    "status": 1,
    "origin_data 原始返回数据":"{\"code\":200,\"userName\":\"\提\莫\队\长\呢\",\"userPortrait\":\"https:\\/\\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\\/ios_images\\/2020-11-26\\/760ED1F5-51C3-40E5-AF7F-6CD6CE969825.jpeg?x-oss-process=image\\/resize,w_200,limit_0\",\"createTime\":\"2021-05-22 18:42:10\"}",
    "record_type": 1,
    "use_time 接口请求耗时ms": 205,
    "created_at 创建时间": "2021-09-27 15:33:24",
    "updated_at": "2021-09-27 15:33:24"
    },
    "average_cost_time 平均响应时间": 0，
    "is_close 发送短信是否关闭":1
    },
    {
    "server_name": "即构",
    "server_type": 1,
    "is_error": 0,
    "right_percent": 0,
    "warning_percent": 0,
    "error_percent": 0,
    "all_num": 0,
    "error_num": 0,
    "warning_num": 0,
    "last_error": [],
    "average_cost_time": 0
    },
    {
    "server_name": "数美",
    "server_type": 3,
    "is_error": 0,
    "right_percent": 0,
    "warning_percent": 0.01,
    "error_percent": 0,
    "all_num": 27,
    "error_num": 0,
    "warning_num": 27,
    "last_error": [],
    "average_cost_time": 0.054
    }
    ]
    }
     */
    public function adminStat()
    {
        $list = app('repo_three_service_check')->adminStat();
        return Response::sendData($list);
    }
    /**
     * @api {get} /three/check/manual/test 手动检测结果
     * @apiVersion 0.1.0
     * @apiName 手动检测结果
     * @apiGroup 三方服务验证
     * @apiParam {int} type 类型 1表示即构 2表示融云 3表示数美
     *
     * @apiSuccessExample 成功响应
     *{
    "code": 200,
    "data": {
    "data": {
    "status": 1,
    "origin_data 第三方接口原始返回数据":"{\"code\":200,\"userName\":\"\提\莫\队\长\呢\",\"userPortrait\":\"https:\\/\\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\\/ios_images\\/2020-11-26\\/760ED1F5-51C3-40E5-AF7F-6CD6CE969825.jpeg?x-oss-process=image\\/resize,w_200,limit_0\",\"createTime\":\"2021-05-22 18:42:10\"}",
    "record_type": 2,
    "use_time 接口请求时间": 76
    },
    "originData": {
    "code": 200,
    "userName": "提莫队长呢",
    "userPortrait": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/ios_images/2020-11-26/760ED1F5-51C3-40E5-AF7F-6CD6CE969825.jpeg?x-oss-process=image/resize,w_200,limit_0",
    "createTime": "2021-05-22 18:42:10"
    },
    "insertDbRes": {
    "isOutTime": 1,
    "sendMsgRes": []
    },
    "is_out_time 是否请求超时 1表示正常 2表示请求超过2秒未超过5秒 3表示超过5秒": 1
    },
    "message": ""
    }
     */
    public function manualTest(Request $request)
    {
        $type = $request->input('type',0);
        if(empty($type)){
            throw new BasicException(0, '请指定手动校验类型');
        }
        $res = app('repo_three_service_check')->manualCheck($type);
        return Response::sendData($res);
    }
    /**
     * @api {get} /three/check/set/close 对应类型是否发送短信
     * @apiVersion 0.1.0
     * @apiName 对应类型是否发送短信
     * @apiGroup 三方服务验证
     *
     * @apiParam {int} type 类型 1表示即构 2表示融云 3表示数美
     * @apiParam {int} is_close 是否关闭 0不关闭 1关闭
     * @apiSuccessExample 成功响应
     *{
    "code": 200,
    "data": [],
    "message": "设置成功"
    }
     */
    public function setSendMsgIsClose(Request $request){
        $type = $request->input('type',0);
        $isClose = $request->input('is_close',0);   //是否关闭
        if(empty($type)){
            throw new BasicException(0, '请指定手动校验类型');
        }
        app('repo_three_service_check')->setSenMsgIsClose($type,$isClose);
        return Response::sendData([],'设置成功');
    }
    /**
     * @api {get} /three/check/record/list 获取对应类型的记录列表
     * @apiVersion 0.1.0
     * @apiName 获取对应类型的记录列表
     * @apiGroup 三方服务验证
     *
     * @apiParam {int} type 类型 1表示即构 2表示融云 3表示数美，必须
     * @apiParam {int} page 页码默认1，必须
     * @apiParam {int} page_size 每页条数 默认10，必须
     * @apiParam {int} is_out_time 是否超时 1不超时 2超时，可选
     * @apiParam {int} record_type 记录类型 1表示定时任务 2表示手动，可选
     * @apiParam {int} min_use_time 最小响应时间，可选
     * @apiParam {int} max_use_time 最大响应时间，可选
     * @apiSuccessExample 成功响应
     *{
    "code": 200,
    "data": {
    "total": 1,
    "list": [
    {
    "id": 37,
    "is_out_time 是否超时 1不超 2超过2秒 3超过5秒": 1,
    "status 接口返回数据状态 1表示正常 2表示异常": 1,
    "origin_data 接口原始数据":"{\"code\":200,\"originData\":{\"code\":1100,\"message\":\"\成\功\",\"requestId\":\"582b74067d238fb8e681c232e104b5a2\",\"score\":212,\"riskLevel\":\"PASS\",\"detail\":\"{\\\"contactResult\\\":[],\\\"contextProcessed\\\":false,\\\"contextText\\\":\\\"\测\试\\\",\\\"description\\\":\\\"\正\常\\\",\\\"model\\\":\\\"M1000\\\",\\\"riskType\\\":0,\\\"sexy_risk_tokenid\\\":0}\",\"status\":0,\"businessLabels\":[]},\"msg\":\"\请\求\成\功\"}",
    "record_type 记录类型1表示定时任务 2表示手动": 1,
    "use_time 接口响应时间": 115,
    "created_at": "2021-09-27 15:32:36",
    "updated_at": "2021-09-27 15:32:36"
    }
    ]
    },
    "message": ""
    }
     */
    public function getRecordLogList(Request $request){
        $param = $request->only( 'is_out_time', 'record_type', 'min_use_time', 'max_use_time');
        $v = app('validator')->make($param, [
            'is_out_time' => 'filled|integer',
            'record_type' => 'filled|integer',
            'min_use_time' => 'filled|integer',
            'max_use_time' => 'filled|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $type = $request->input('type',0);
        $page = $request->input('page',1);
        $pageSize = $request->input('page_size',10);
        $page = $page<1?1:$page;
        $pageSize = $pageSize<0?10:$pageSize;
        $list = app('repo_three_service_check')->getRecordLogList($type,$page,$pageSize,$param);
        return Response::sendData($list);
    }
}
